classdef ET_GEN < handle
    
    properties

	text_size = 12

        % Eyelink switch
        
        EyeLink = 0
        
        % Screen properties
        
        scn = struct(...
            'mon',1, ... % 0 or 1
            'res',6, ...
            'w',1600, ... % 1 = 640x480; 3 = 1024x768
            'h',1200, ...
            'refresh_rate',60, ...% Refresh rate in Hz
            'wcm',40.8, ... % Width in centimenters
            'hcm',45.0, ... % Height in centimenter
            'dist',60, ... % Distance from scn to subject in cm
            'color',32, ... % Color used in bits
            'bg',[0 0 0], ... % Background color
            'p',0) % Pointer to the screen
             
        % Date for time stamp
        
        date
        
        % Experiment information
        
        exp_par = struct()

        
        % Eyelink Pointer
        
        el_pointer
        
        % New clut for the experiment
        
        new_clut
        
        % Old clut to come back to normality
        
        old_clut
        
        % Conditions
        
        cond
        
    end
    
    properties (SetAccess = protected)
        
        % Assumes a block - trial design
               
        blocks
        trials
        
        num_blocks
        num_trials
        
        
    end
    
    methods
        
        
        function self = ET_GEN()
            self.date = clock;                        
        end
        
        function runScreen(self)
            % Runs the screen. Because it depends on the GM in this case it
            % is only a place holder
        end
        
        function startGM(self)
            % Initializes the graphical motor, i.e., cogent or PTB. It is
            % only a place holder.
        end
        
        function prepareBlocks(self)
            % Initializes the block. This is only a place holder.
        end        
        
        function prepareTrials(self)
            % Initializes the trials. This is only a place holder.
        end
        
        function trialTransition(self)
            % Specifies the transition from one trial to other. In this
            % case it is a place holder.
        end
        
        function send_parameters(self,trial)
           % Send information the eyetracker so it stores it in the edf
           % file. This is a place holder.
        end
        
        
        function startDialog(self)
            % Dialog that starts before running the experiment.

            prac = input('Practice(y/n)? ','s');

            if prac == 'y';
                self.exp_par.sbj = 0; self.exp_par.exp = 0;

            else
                self.exp_par.sbj = input('Subject number? ');
                % self.exp_par.exp = input('Experiment number? ');
            end
            
        end
        
        function set_clut(self)
            % Sets the clut and stores the old clut
            if ~isempty(self.new_clut)
                self.old_clut = Screen('LoadNormalizedGammaTable',...
                    self.scn.p,self.new_clut);
            end
        end
        
        function exit(self,Err)
            if self.EyeLink
                Eyelink('Shutdown')
            end
            if isunix()
                ListenChar(0);
            elseif ispc()
            end
            
            if  ~isempty(self.old_clut)
                Screen('LoadNormalizedGammaTable',self.scn.p,self.old_clut);
            end                
            
            Screen('CloseAll');
            Priority(0);
            if nargin > 1
                rethrow(Err)
            end

        end
        
        function centeredText(self,text,color,ts)
            % Wait to have any input key pressed
            %
            % TEXT     Text that will be displayed.
            % COLOR    Color of the text which will be displayed. Defaults
            %          to white.
            % TS       Text size.
            
            if nargin < 3
                color = [255 255 255];
            end
            if nargin > 3
                ots =Screen('TextSize', self.scn.p, ts);
            end

            textrect = Screen('TextBounds', self.scn.p, text);
            [xoffset, yoffset] = RectCenter(textrect);
            Screen(self.scn.p, 'DrawText', text,...
                self.scn.w/2-xoffset,self.scn.h/2-yoffset,color);
            Screen(self.scn.p, 'Flip');
            if nargin > 3
                Screen('TextSize', self.scn.p, ots);
            end
                
            
        end
        
        function [goout,kn]= escapeTrial(self)
            % TO IMPROVE   
            goout = 0; kn = 0; avail = KbCheck;
            if avail
                centeredText(self,['Press ''q'' to quit.' ...
                    ' Press any other key to continue. '])
                WaitSecs(0.5); % Buffer
                while 1
                    [avail,~,k] = KbCheck();
                    if avail; break; end
                end
                % If the key is q
                if find(k) == KbName('q')
                    goout = 1; kn = 1;
                else
                    WaitSecs(0.3);
                    return
                end

            end
        end
        
    end
end
